programming4us
           
 
 
SQL Server

SQL Server 2008 : Developing Custom Managed Database Objects (part 1)

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
3/23/2011 9:16:21 AM
SQL Server 2008 hosts the common language runtime (CLR), implementing what’s known as the Hosting API. The Hosting API gives SQL Server 2008 full control over the execution of .NET code in a carefully managed environment that honors the shared resource usage of both SQL Server and the CLR. The CLR provides an execution context far safer than that of code you might formerly have run in an extended stored procedure or COM object under SQL Server 2000 and previous editions.

In the sections that follow, you will create one of each of the managed versions of database routines and types. You work with both the SQL Server project type in Visual Studio 2008 as well as the T-SQL Data Definition Language (DDL) syntax for managed objects.

Finally, you’ll learn about advanced topics such as transaction control in mixed (T-SQL and managed) environments.

An Introduction to Custom Managed Database Objects

The capability to run managed code presents a world of possibilities, yet these features must be leveraged appropriately. The meaning of appropriate will ultimately be the result of ongoing dialogs between database administrators and the developers who want to use the .NET Framework in SQL Server.

Just like SQL Server’s capability to host web services , this feature set begins to blur the line between SQL Server as a database server and SQL Server as a lightweight application server.

.NET assemblies are built using Visual Studio or the command-line compilers and then literally uploaded into the database and loaded into memory on the same physical server as the SQL Server instance. CLR objects may therefore consume valuable server and network resources.

This scenario presents a challenging new management paradigm that database administrators, managers, and developers have to negotiate. Administrators are just beginning to consider strategies for what kinds of .NET code they should allow to run and in which contexts. Following are a few general rules to consider regarding when managed objects should and should not be used:

  • Data selection and modification should always be performed using T-SQL because that’s what it’s optimized to do. You should not create a T-SQL wrapper in your .NET code.

  • You should use managed code when you need to overcome the procedural limitations of T-SQL, such as avoiding the use of nested cursors that connect to multiple databases and other awkward constructs. (SQL was never developed to be a procedural language, only a set-based query language.)

  • You should use managed code when you want to extend the per-row or per-set effects of routines to leverage managed resources, such as XML parsers, web services, and custom code libraries.

The software development staff still must decide what to do, but we can be thankful that SQL Server has some rules of its own for what kinds of operations can be called and under which permission sets, as discussed in the following section.

Managed Object Permissions

The first thing to know about managed object permissions is that SQL Server has only blessed a certain group of assemblies usable under each of the three SQL Server permission sets.

Figure 1 shows the Add References dialog for a SQL Server project in Visual Studio 2008, listing these .NET Framework assemblies. They are the only assemblies (aside from user-created assemblies) that can be referenced in SQL Server projects. Note that this list doesn’t change in Visual Studio, regardless of the permission set used. Note also that SQL Server and/or Visual Studio walks down the reference chain to see whether any referenced assemblies reference anything that is not blessed. Therefore, you shouldn’t bother trying to get around this list; there isn’t even a Browse button on the dialog box as there is with the other project types.

Figure 1. Blessed assemblies in the Add References dialog in Visual Studio 2008.


The Three Permission Sets

SQL Server has three built-in .NET Code Access Security (CAS) permission sets that define which kinds of operations can be executed at runtime. Using the CAS layer is a huge improvement over running extended stored procedures under default login credentials because it allows for fine-grained permission granting and revocation.

These are the permission sets, in increasing order of freedom:

  • SAFE

  • EXTERNAL_ACCESS

  • UNSAFE

These keywords are used in the DDL syntax for assemblies.

Assuming that you have built an assembly targeted for SQL Server use (which you do in the next section), you can use the following syntax for loading that assembly into your database of choice:

CREATE ASSEMBLY AssemblyName [AUTHORIZATION LoginName]
FROM StringPathToAssemblyDll | BinaryDataValue
[WITH PERMISSION_SET (SAFE | EXTERNAL_ACCESS | UNSAFE) ]

This syntax is reasonably self-explanatory: you tell SQL Server the name of the assembly and the path (using a UNC if needed) to it. If you’re loading an assembly from a varbinary column, you supply the actual data that makes up the compiled code of the assembly instead of the path to it (Visual Studio does this).

Note

CREATE ASSEMBLY and ALTER ASSEMBLY are commands used by Visual Studio’s Deploy feature, which does the managed code DDL work for you.


The WITH PERMISSION SET clause is optional, and it defaults to SAFE. Marking an assembly with the SAFE permission set indicates that no external resources (for example, the Registry, web services, file I/O) are going to be accessed. The DDL will fail if assemblies such as System.IO are referenced, and anything causing a permission demand for executing similar operations will result in an exception being thrown at runtime. Marking an assembly with the EXTERNAL_ACCESS permission set tells SQL Server that it will be using resources such as networking, files, and so forth. Assemblies such as System.Web.Services (but not System.Web) may be referenced with this set.

Marking an assembly with the UNSAFE permission set tells SQL Server that not only might external resources be used, but unmanaged code may even be invoked from managed code.

Some assemblies in the .NET Framework go so far as to tell the processes that ultimately host them (such as SQL Server or Internet Explorer) about their relative safety, using a specific .NET attribute: HostProtectionAttribute (HPA).

The enumeration flags of the HPA’s parameter indicate to the host what kinds of operations the classes decorated with it may attempt. Because documentation of the HPA with regards to SQL Server is scant, it’s unclear whether SQL Server ultimately relies on the HPA to determine what may be loaded. (It seems to do so at runtime, but the blessed list is likely to be hard coded.)

Following are some of the operations you cannot perform with .NET code running under SQL Server’s SAFE and EXTERNAL_ACCESS options (but possibly under UNSAFE):

  • Thread synchronization

  • External process management

  • Framework security changes

  • Use of non-read-only static fields

Only those in the sysadmin role can upload UNSAFE assemblies to SQL Server. (Just don’t tell your DBA we told you how to do it.)

The EXTERNAL_ACCESS permission on master is required for uploading EXTERNAL_ACCESS assemblies. And anyone in the db_owner role may load SAFE assemblies.

Developing Managed Objects with Visual Studio 2008

When SQL Server 2008 is installed, it includes Microsoft.SqlServer.Server, the assembly that contains the attributes and other classes needed for SQLCLR (the common acronym for managed code running in SQL Server) programming.

The first step in working with SQLCLR is to enable the feature in SQL Server.

Setting Up the Server for Managed Code Execution

Before you can work with managed database objects, you need to execute the following T-SQL commands in the context of the master database:

sp_configure 'clr enabled', 1
RECONFIGURE
go

This step is necessary because SQL Server comes with managed code execution turned off by default.

At this point, you’re ready to create your first SQLCLR project using Visual Studio 2008 (VS). Start VS and create a new C#-based SQL Server project with a name of your choosing (the example is named SQL2008SQLCLR). Figure 2 shows the Add New Project dialog.

Figure 2. Using the Add New Project dialog in Visual Studio 2008 to create a SQLCLR project.

Next, VS asks you to create or add a reference to a database to which you will deploy your new assembly. During deployment, your assembly (and with it, all its SQLCLR types and routines) is uploaded directly into SQL Server’s system tables. Select AdventureWorks2008 so you can work with the examples that follow.

Next, you create your first managed SQL Server object, a stored procedure written in C#.

Other -----------------
- SQL Server 2008 : Profiler Usage Scenarios (part 2)
- SQL Server 2008 : Profiler Usage Scenarios (part 1) - Analyzing Slow Stored Procedures or Queries & Deadlocks
- SQL Server 2008 : Defining Server-Side Traces
- SQL Server 2008 : SQL Server Profiler - Replaying Trace Data
- SQL Server 2008 : SQL Server Profiler - Saving and Exporting Traces
- SQL Server 2008 : SQL Server Profiler - Creating Traces
- SQL Server 2008 : SQL Server Profiler Architecture
- SQL Server 2008: Administering Database Objects - Working with Tables (part 7) - Partitions
- SQL Server 2008: Administering Database Objects - Working with Tables (part 6) - Compression
- SQL Server 2008: Administering Database Objects - Working with Tables (part 5) - Sparse Columns
- SQL Server 2008: Administering Database Objects - Working with Tables (part 4) - Check Constraints
- SQL Server 2008: Administering Database Objects - Working with Tables (part 3) - Foreign Key Constraints
- SQL Server 2008: Administering Database Objects - Working with Tables (part 2) - Primary Key Constraints & Unique Constraints
- SQL Server 2008: Administering Database Objects - Working with Tables (part 1) - Default Constraints
- SQL Server 2008: Administering Database Objects - Working with Database Snapshots
- Programming with SQL Azure : WCF Data Services (part 3)
- Programming with SQL Azure : WCF Data Services (part 2) - Creating the Client Application
- Using XML in SQL Server 2008: Relational Data As XML - The FOR XML Modes (part 4) - EXPLICIT Mode
- Using XML in SQL Server 2008: Relational Data As XML - The FOR XML Modes (part 3) - AUTO Mode
- Programming with SQL Azure : WCF Data Services (part 1)
 
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
programming4us programming4us